package top.malegod.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.malegod.model.Manage;
import top.malegod.service.ManageService;
import top.malegod.utils.ServerResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/28 19:41
 * @description: 管理员的一些操作
 */
@Controller
@RequestMapping("/manage")
public class ManageController {

  @Autowired
  private ManageService manageService;

  //==============================统计接口================================================

  /**
   * 统计用户、订单、社区、评论数量
   * @return
   */
  @RequestMapping(value = "/baseCount.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageBaseCount(){
    ServerResponse response = manageService.manageBaseCount();
    return response;

  }

  //==============================用户接口================================================

  /**
   * 管理员登陆
   * @param username
   * @param password
   * @param session
   * @return
   */
  @RequestMapping(value = "/login.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageLogin(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "password") String password,
      HttpSession session
  ) {
    ServerResponse response = manageService.manageLogin(username, password);
    if (response.success()) {
      session.setAttribute("manage", response.getData());
    }
    return response;
  }

  /**
   * 判断管理员是否登录
   * @param session
   * @return
   */
  @RequestMapping(value = "/manageInfo.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageInfo(HttpSession session) {
    Manage manage = (Manage) session.getAttribute("manage");
    if (manage == null) {
      return ServerResponse.createError("你还未登陆,请先登录");
    }
    manage.setPassword(null);
    return ServerResponse.createSuccess(manage);
  }

  /**
   * 管理员退出登陆
   * @param session
   * @return
   */
  @RequestMapping(value = "/loginOut.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageLoginOut(HttpSession session) {
    Manage manage = (Manage) session.getAttribute("manage");
    if (manage == null) {
      return ServerResponse.createError("你还未登陆,请先登录");
    }
    session.removeAttribute("manage");
    manage = (Manage) session.getAttribute("manage");
    if (manage == null) {
      return ServerResponse.createSuccess("退出成功");
    }
    return ServerResponse.createError("退出失败");
  }

  /**
   * 获取所有用户列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/user/list.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageUserList(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ) {
    ServerResponse response = manageService.manageUserList(pageNum, pageSize);

    return response;
  }

  /**
   * 删除用户
   * @param id
   * @return
   */
  @RequestMapping(value = "/user/delete.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageDeleteUser(String id) {

    ServerResponse response = manageService.manageDeleteUser(id);

    return response;
  }

  /**
   * 获取恢复用户列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/user/restoreList.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageRestoreUserList(
          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ) {
    ServerResponse response = manageService.managerRestoreUserList(pageNum, pageSize);

    return response;
  }

  /**
   * 用户恢复
   * @param id
   * @return
   */
  @RequestMapping(value = "/user/restore.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageRestoreUser(String id) {
    ServerResponse response = manageService.manageRestoreUser(id);
    return response;
  }

  //==============================订单接口================================================

  /**
   * 订单列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/order/list", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageOrderList(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ) {

    ServerResponse response = manageService.manageOrderList(pageSize, pageNum);
    return response;
  }

  /**
   * 订单删除
   * @param id

   * @return
   */
  @RequestMapping(value = "/order/delete.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageOrderDelete(String id){
    ServerResponse response = manageService.manageOrderDelete(id);
    return response;
  }

  //==============================社区接口================================================

  /**
   * 社区列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/community/list.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageCommunityList(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ) {
    ServerResponse response = manageService.manageCommunityList(pageSize, pageNum);
    return response;
  }

  /**
   * 社区搜索
   * @param pageSize
   * @param pageNum

   * @return
   */
  @RequestMapping(value = "/community/search.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageCommunitySearch(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
      String content
  ) {
    ServerResponse response = manageService.manageCommunitySearch(pageSize, pageNum,content);
    return response;
  }

  /**
   * 删除社区分享
   * @param id
   * @return
   */
  @RequestMapping(value = "/community/delete.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageDeleteCommunity(String id) {
    ServerResponse response = manageService.manageDeleteCommunity(id);
    return response;
  }


  //==============================评论接口================================================

  /**
   * 评论列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/comments/list", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageCommentsList(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ) {

    ServerResponse response = manageService.manageCommentsList(pageSize, pageNum);
    return response;
  }

  /**
   * 评论删除
   * @param id
   * @return
   */
  @RequestMapping(value = "/comments/delete.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageCommentsDelete(String id){
    ServerResponse response = manageService.manageCommentsDelete(id);
    return response;
  }

  //==============================其他接口================================================

  /**
   * 分类列表
   * @return
   */
  @RequestMapping(value = "/other/categoryList.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageCategoryList(){

    ServerResponse response = manageService.manageCategoryList();
    return response;
  }

  /**
   * 增加分类
   * @param name
   * @return
   */
  @RequestMapping(value = "/other/addCategory.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageAddCategory(String name){
    ServerResponse response = manageService.manageAddCategory(name);
    return response;
  }

  /**
   * 修改分类名
   * @param id
   * @param name
   * @return
   */
  @RequestMapping(value = "/other/updateCategory.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageUpdateCategory(
      @RequestParam(value = "id") Integer id,
      @RequestParam(value = "name") String name
  ){
    ServerResponse response = manageService.manageUpdateCategory(id, name);
    return response;
  }

  /**
   * 删除分类
   * @param id
   * @return
   */
  @RequestMapping(value = "/other/deleteCategory.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageDeleteCategory(Integer id){

    ServerResponse response = manageService.manageDeleteCategory(id);
    return response;
  }

  /**
   * 积分列表
   * @param pageSize
   * @param pageNum
   * @return
   */
  @RequestMapping(value = "/other/integralList.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageIntegralList(
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
  ){
    ServerResponse response = manageService.manageIntegralList(pageNum, pageSize);
    return response;
  }

  /**
   * 修改积分
   * @param count
   * @param userId
   * @return
   */
  @RequestMapping(value = "/other/updateIntegral.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageUpdateIntegral(
      @RequestParam("count") Double count,
      @RequestParam("userId") String userId
  ){
    ServerResponse response = manageService.manageUpdateIntegral(count, userId);
    return response;
  }

  /**
   * 等级列表
   * @return
   */
  @RequestMapping(value = "/other/rankList.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageRankList(){

    ServerResponse response = manageService.manageRankList();
    return response;
  }


  /**
   * 等级修改
   * @param id
   * @param name
   * @return
   */
  @RequestMapping(value = "/other/updateRank.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageUpdateRank(
      @RequestParam(value = "id") Integer id,
      @RequestParam(value = "name") String name
  ){
    ServerResponse response = manageService.manageUpdateRank(id, name);
    return response;
  }

  /**
   * 删除等级
   * @param id
   * @return
   */
  @RequestMapping(value = "/other/deleteRank.do", method =  RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageDeleteRank(Integer id){
    ServerResponse response = manageService.manageDeleteRank(id);
    return response;
  }

  /**
   * 增加等级
   * @param name
   * @return
   */
  @RequestMapping(value = "/other/addRank.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse manageAddRank(String name){
    ServerResponse response = manageService.manageAddRank(name);
    return response;
  }


}
